package com.neusoft.neuiotms.dcom.repository.impl;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.neusoft.bizcore.web.jpa.repository.AbstractRepository;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.model.Handover;
import com.neusoft.neuiotms.dcom.model.QHandover;
import com.neusoft.neuiotms.dcom.repository.CustomizedHandoverRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQuery;

public class CustomizedHandoverRepositoryImpl extends AbstractRepository implements CustomizedHandoverRepository {

    @Override
    public List<Handover> index(final Searchable searchable) {
        final QHandover qHandover = QHandover.handover;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<Handover> query = this.query().selectFrom(qHandover);
        query.where(where);
        return query.fetch();
    }

    @Override
    public Page<Handover> search(final Pageable pageable, final Searchable searchable) {
        final QHandover qHandover = QHandover.handover;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<Handover> query = this.query().selectFrom(qHandover);
        query.where(where);
        return super.search(query, pageable);
    }

    @Override
    protected Class<?> getModelClass() {
        return Handover.class;
    }

    private BooleanBuilder buildWhere(final Searchable searchable) {
        final BooleanBuilder where = new BooleanBuilder();
        final QHandover qHandover = QHandover.handover;

        // 组织机构
        where.and(super.equalsStr(qHandover.orgCode, searchable, "orgCode"));
        // 职能ID
        where.and(super.equalsLong(qHandover.functionalGroup.id, searchable, "functionalGroupId"));
        // in组织机构
        where.and(super.inStrings(qHandover.orgCode, searchable, "orgCodes"));
        // in职能组
        where.and(super.inLongs(qHandover.functionalGroup.id, searchable, "functionalGroupIds"));

        return where;
    }
}
